package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCCommissionYearConfig;
import com.shrct.businessprocess.project.RCSaleCommissionYearPayment;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.impl.RCSaleCommissionYearPaymentImpl;
import com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl;
import com.shrct.businessprocess.project.proxy.RCCommissionYearConfigProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCCOMMISSIONYEARCONFIG_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rccommissionyearconfig")
public class RCCommissionYearConfigImpl extends BaseModelObject implements RCCommissionYearConfig {
  public RCCommissionYearConfigImpl() {
    super();
  }
  
  private Integer year;
  
  public Integer getYear() {
    return this.year;
  }
  
  public RCCommissionYearConfig setYear(final Integer year) {
    this.year = year;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCSalesCommissionYearConfigImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "overallConfig")
  private Collection<RCSalesCommissionYearConfig> items = new java.util.ArrayList<com.shrct.businessprocess.project.RCSalesCommissionYearConfig>();;
  
  public Collection<RCSalesCommissionYearConfig> getItems() {
    return this.items;
  }
  
  public RCSalesCommissionYearConfig createAndAddtoItems() {
    getItems().size();
    com.shrct.businessprocess.project.RCSalesCommissionYearConfig rCSalesCommissionYearConfig = new com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl(this);
    getObjectFactory().create(rCSalesCommissionYearConfig);
    getItems().add(rCSalesCommissionYearConfig);
    return rCSalesCommissionYearConfig;
    
  }
  
  public RCSalesCommissionYearConfig createAndAddtoItems(final Procedure1<RCSalesCommissionYearConfig> updater) {
    getItems().size();
    com.shrct.businessprocess.project.RCSalesCommissionYearConfig rCSalesCommissionYearConfig = new com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl(this);
    getObjectFactory().create(rCSalesCommissionYearConfig);
    getItems().add(rCSalesCommissionYearConfig);
    return rCSalesCommissionYearConfig;
    
  }
  
  public RCCommissionYearConfig removeAllItems() {
    for(RCSalesCommissionYearConfig obj : this.items){
    	getObjectFactory().delete(obj);
    }
    this.items.clear();
    return this;	
    
  }
  
  public RCCommissionYearConfig removeFromItems(final RCSalesCommissionYearConfig rCSalesCommissionYearConfig) {
    this.items.remove(rCSalesCommissionYearConfig);
    getObjectFactory().delete(rCSalesCommissionYearConfig);
    return this;
    
  }
  
  public RCSalesCommissionYearConfig createSalesConfig(final Procedure1<? super RCSalesCommissionYearConfig> init) {
    RCSalesCommissionYearConfig _createAndAddtoItems = this.createAndAddtoItems();
    final Procedure1<RCSalesCommissionYearConfig> _function = new Procedure1<RCSalesCommissionYearConfig>() {
        public void apply(final RCSalesCommissionYearConfig item) {
          init.apply(item);RCSaleCommissionYearPayment rCSaleCommissionYearPayment=RCSaleCommissionYearPaymentImpl.create(getObjectFactory());
          final Procedure1<RCSaleCommissionYearPayment> _function = new Procedure1<RCSaleCommissionYearPayment>() {
              public void apply(final RCSaleCommissionYearPayment it) {
                it.setYear(RCCommissionYearConfigImpl.this.year);
                RCSalesAssignment _saler = item.getSaler();
                it.setSaler(_saler);
              }
            };
          ObjectExtensions.<RCSaleCommissionYearPayment>operator_doubleArrow(rCSaleCommissionYearPayment, _function);
          RCCommissionYearConfigImpl.this.checkValid();
        }
      };
    RCSalesCommissionYearConfig _doubleArrow = ObjectExtensions.<RCSalesCommissionYearConfig>operator_doubleArrow(_createAndAddtoItems, _function);
    return _doubleArrow;
  }
  
  public void deleteSalesConfig(final RCSalesCommissionYearConfig config) {
    this.removeFromItems(config);
  }
  
  public void checkValid() {
    boolean _notEquals = (!Objects.equal(this.year, null));
    Validates.Assert(_notEquals, "\u5E74\u5EA6\u5FC5\u987B\u8BBE\u7F6E");
    IGenericQuery<Long> query=getObjectFactory().createGenericQuery(Long.class,"select count(*) from com.shrct.businessprocess.project.impl.RCCommissionYearConfigImpl as e   where ((e.year) = (:year)) and  e.active = 1      ").setParameter("year",year);
    Long _uniqueResult = query.uniqueResult();
    boolean _equals = ((_uniqueResult).longValue() == 1);
    Validates.Assert(_equals, "\u5E74\u5EA6\u5FC5\u987B\u552F\u4E00");
  }
  
  public static RCCommissionYearConfig create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCCommissionYearConfig rCCommissionYearConfig = new com.shrct.businessprocess.project.impl.RCCommissionYearConfigImpl(
    );
    objectFactory.create(rCCommissionYearConfig);
    return rCCommissionYearConfig;			
    
  }
  
  public static RCCommissionYearConfig createRCCommissionYearConfigImpl(final IObjectFactory objectFactory, final Procedure1<RCCommissionYearConfig> init) {
    com.shrct.businessprocess.project.RCCommissionYearConfig rCCommissionYearConfig = new com.shrct.businessprocess.project.impl.RCCommissionYearConfigImpl(
    );
    objectFactory.create(rCCommissionYearConfig);
    init.apply(rCCommissionYearConfig);
    
    return rCCommissionYearConfig;			
    
  }
  
  public RCCommissionYearConfigProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCCommissionYearConfigProxy proxy = new com.shrct.businessprocess.project.proxy.RCCommissionYearConfigProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
